package com.qwiki.xml;

import java.io.PrintStream;

import com.qwiki.dict.CategoryDictionary;
import com.qwiki.dict.TextDictionary;

public class TextPageAsyncHandler extends PageAbstractAsyncHandler<String[], PageFilter<String[]>> {
  // printer has to be thread safe
  private final PrintStream printer;
  private final CategoryDictionary goodCategories;
  private final TextDictionary goodWords;

  public TextPageAsyncHandler(String name, int id, CategoryDictionary cdict, TextDictionary dict, PrintStream printer) {
    super(name, id);
    this.printer = printer;
    this.goodCategories = cdict;
    this.goodWords = dict;
  }

  @Override
  public void doProcess(String[] page) {
    String title = page[0];
    String text = page[1];
    String[] categories = page[2].split(" ");

    String[] raw = text.split(" ");
    String[] good = goodWords.filter(raw);
    StringBuilder sb = new StringBuilder();
    for (String word : good) {
      if (word.length() > 0)
        sb.append(" ").append(word);
    }
    text = sb.toString();
    if (text.length()==0) return;
    for (String category : categories) {
      if (goodCategories.contains(category))
        printer.println(category + " " + text);
    }
  }

  @Override
  public PageFilter<String[]> getFilter() {
    return null;  //To change body of implemented methods use File | Settings | File Templates.
  }

  @Override
  public void setFilter(PageFilter<String[]> filter) {
  }
}
